<template>
	<view>
		<u-form :model="form" ref="uForm">
			<u-form-item label="问题:">
				<u-input v-model="question.description" disabled />
			</u-form-item>
			<u-form-item label="回答:">
				<u-input v-model="answerVo.answer.answerDetails" />
			</u-form-item>
			<u-upload ref="uUpload" :action="action" :auto-upload="false" :on-success="success" :on-change="change"></u-upload>
			<u-button @click="submit">提交</u-button>
			<u-toast ref="uToast" />
		</u-form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				answerVo:{
					answer:{
						answerDetails:"",
						questionId:"",
					},
					imageUrl:"",
					page:"",
					
				},
				form: {
					description: '',
				},
				question:[],
				action: 'http://43.136.183.146:9090/upload/image',
				showUploadList: false,
				// 这里直接获取内部的lists变量即可
				lists: [],
				id:-1,
			};
		},
		// 只有onReady生命周期才能调用refs操作组件
		onReady() {
			// 得到整个组件对象，内部图片列表变量为"lists"
			this.lists = this.$refs.uUpload.lists;
		},
		onLoad(options) {
			if(options==null){
				return;
			}
			this.id = options.id;
			this.answerVo.answer.questionId=options.id;
			
		},
		
		created() {		
			if(this.id==-1){
				return;
			}
			this.$axios.get("/api/life/question/getById?id="+this.id).then(r => {
				console.log(r);
				this.question=r.data.data;
				
				});
			console.log(this.id)
		},
		methods: {
			showToast() {
							this.$refs.uToast.show({
								title: '提交成功',
								type: 'success',
								url: '/pages/life/addQuestion'
							})
			},
			onPageto(url) {
				uni.navigateTo({
					url: "/pages/life/question"
				})
			},
			submit()  {
				this.$refs.uUpload.upload();
				console.log(this.answerVo.answer.answerDetails);
				if(this.answerVo.answer.answerDetails.length<5){
					this.$refs.uToast.show({
						title: '回答字数不低于5',
						type: 'warning',
					})
					return;
				}
						
				this.$axios.post("/api/life/answer/add",this.answerVo).then(r => {
						if(r.data.code==200){
							this.showToast();	
						}else{
							this.$refs.uToast.show({
								title: '出错啦！',
								type: 'error',
							})
						}
					});
				},
			success(data, index, lists, name) {
				console.log(data);
			},
			change(res, index, lists, name){
				console.log(res);
				
			}
		}
	};
</script>

<style>
</style>